import java.util.ArrayList;
import java.util.List;

/**
 * @author LKQ
 * @date 2021/12/25 21:09
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.convert("PAYPALISHIRING", 3);
    }

    public String convert(String s, int numRows) {

        if (numRows == 1) {
            return s;
        }
        // 每一行存储一个字符串
        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < Math.min(numRows, s.length()); i++) {
            rows.add(new StringBuilder());
        }
        // 当前行
        int curRow = 0;
        boolean goingDown = false;

        for (char c : s.toCharArray()) {
            rows.get(curRow).append(c);
            // 第1行，goingDown = true, 行+1，最后一行，goingDown = false，往上，行-1
            if (curRow == 0 || curRow == numRows - 1) {
                goingDown = !goingDown;
            }
            curRow += goingDown ? 1 : -1;
        }

        StringBuilder ret = new StringBuilder();
        for (StringBuilder row : rows) {
            ret.append(row);
        }
        return ret.toString();
    }

}
